package com.netintech.project.system.ffmpeg;

import lombok.extern.slf4j.Slf4j;
import net.bramp.ffmpeg.FFmpeg;
import net.bramp.ffmpeg.FFmpegExecutor;
import net.bramp.ffmpeg.FFmpegUtils;
import net.bramp.ffmpeg.FFprobe;
import net.bramp.ffmpeg.builder.FFmpegBuilder;
import net.bramp.ffmpeg.job.FFmpegJob;
import net.bramp.ffmpeg.probe.FFmpegProbeResult;
import net.bramp.ffmpeg.probe.FFmpegStream;
import net.bramp.ffmpeg.progress.Progress;
import net.bramp.ffmpeg.progress.ProgressListener;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author luda
 * @date 2021/3/24 15:06
 */
@Slf4j
public class aaa {
    final FFmpeg ffmpeg = new FFmpeg();
    final FFprobe ffprobe = new FFprobe();

    final Locale locale = Locale.US;


    public aaa() throws IOException {
    }

    public void devideByCommand(String filename, String directory, String uuid) throws IOException {

        FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);

        FFmpegProbeResult probe = ffprobe.probe(filename);
//
//        final List<FFmpegStream> streams = probe.getStreams().stream().filter(
//                fFmpegStream -> fFmpegStream.codec_type != null).collect(Collectors.toList());
//
//        final Optional<FFmpegStream> audioStream = streams.stream().filter(
//                fFmpegStream -> FFmpegStream.CodecType.AUDIO.equals(fFmpegStream.codec_type)).findFirst();

//        if (!audioStream.isPresent()) {
//            log.error("未发现音频流");
//        }
//        FFmpegStream stream2 = new FFmpegStream();

        String output = String.format("%s%sstream.m3u8", directory, File.separator);

        FFmpegBuilder builder = new FFmpegBuilder()
                .setInput(filename)
//                .overrideOutputFiles(true)
//                .setFormat("wav")
//                .setAudioBitRate(audioStream.isPresent() ? audioStream.get().bit_rate : 0)
//                .setAudioChannels(1)
//                .setAudioCodec("aac")        // using the aac codec
//                .setAudioSampleRate(audioStream.get().sample_rate)
//                .setAudioBitRate(audioStream.get().bit_rate)
//                .setStrict(FFmpegBuilder.Strict.STRICT)
//                .setFormat("hls")
//                .addExtraArgs("-hls_wrap", "0", "-hls_time", "5", "-hls_list_size", "0")
//                .setInput(new FFmpegProbeResult())
                .addOutput("D:\\aa22.mp4")
//                .addExtraArgs("-i", "rtsp://admin:ks123456@172.16.11.172:554/h264/ch1/main/av_stream")
                .done();


        FFmpegJob job =
                executor.createJob(
                        builder,
                        new ProgressListener() {

                            // Using the FFmpegProbeResult determine the duration of the input
//                            final double duration_ns = probe.getFormat().duration * TimeUnit.SECONDS.toNanos(1);

                            @Override
                            public void progress(Progress progress) {
//                                double percentage = progress.out_time_ns / duration_ns;

                                // Print out interesting information about the progress
                                String consoleLog = String.format(
                                        locale,
                                        "[%.0f%%] status:%s frame:%d time:%s fps:%.0f speed:%.2fx",
//                                        percentage * 100,
                                        progress.status,
                                        progress.frame,
                                        FFmpegUtils.toTimecode(progress.out_time_ns, TimeUnit.NANOSECONDS),
                                        progress.fps.doubleValue(),
                                        progress.speed);
                                log.debug(consoleLog);
                            }
                        });

        job.run();

        if (job.getState() == FFmpegJob.State.FINISHED) {
            log.error("文件分割成功");
        } else {
            log.error("文件分割发生不可预料的错误:{}");
        }

    }


}
